package com.twu.biblioteca;

import com.google.common.collect.ImmutableMap;

import java.util.List;
import java.util.Map;
import java.util.Scanner;

import static com.google.common.collect.FluentIterable.from;
import static com.google.common.collect.ImmutableList.of;

public class MainMenu {

    public static final String WELCOMING_MESSAGE = "Welcome to Biblioteca library!";
    public static final String SELECTION_INPUT_PROMPT = "Please input your selection:";
    public static final String INVALID_OPTION_MESSAGE = "Select a valid option!!";
    public static final String VIEW_BOOKS = "VIEW ALL BOOKS";
    public static final String MEMBERSHIP_INQUIRY = "MEMBERSHIP INQUIRY";
    public static final String RESERVE_BOOKS = "RESERVE BOOKS";
    private static final String VIEW_MOVIES = "VIEW ALL MOVIES";
    public static final String LOGIN = "LOGIN";
    private List<String> menuOptionsAfterLogin;
    private List<String> menuOptionsBeforeLogin;
    private ImmutableMap<String, Handler> commandHandler = ImmutableMap.of(VIEW_BOOKS, new ViewBookHandler(),
            MEMBERSHIP_INQUIRY, new MembershipInquiryHandler(), RESERVE_BOOKS, new ReserveBooksHandler(), VIEW_MOVIES,
            new ViewMoviesHandler(), LOGIN, new LoginHandler());

    public MainMenu() {
        menuOptionsBeforeLogin = of(VIEW_BOOKS, MEMBERSHIP_INQUIRY, VIEW_MOVIES, LOGIN);
        menuOptionsAfterLogin = of(VIEW_BOOKS, RESERVE_BOOKS, MEMBERSHIP_INQUIRY, VIEW_MOVIES);
    }

    public String getWelcomeMessage() {
        return WELCOMING_MESSAGE;
    }

    public List<String> getMenuOptionsBeforeLogin() {
        return menuOptionsBeforeLogin;
    }

    public List<String> getMenuOptionsAfterLogin() {
        return menuOptionsAfterLogin;
    }

    public void handleUserInput() {
        String userInput = getUserInput(SELECTION_INPUT_PROMPT);
        if (commandHandler.containsKey(userInput)) {
            System.out.println(commandHandler.get(userInput).handle());
        } else {
            System.out.println(INVALID_OPTION_MESSAGE);
        }
    }

    private String getUserInput(String inputPrompt) {
        System.out.print(inputPrompt);
        Scanner scanner = new Scanner(System.in);
        return scanner.nextLine();
    }
}

